<?php
/**
 * Layout class
 * 
 * @author Mathieu Pheulpin - sokaru91@gmail.com
 *
 */
class Mac_Layout
{
    const DEFAULT_LAYOUT_NAME = 'default';
    
    /**
     * Layout's name
     * @var string
     */
    private $name = '';
    
    /**
     * Layout directory path
     * @var string
     */
    private $path = null;
    
    /**
     * Title used in the <title> marker
     * @var string
     */
    private $headTitle = null;
    
    /**
     * Script paths to be included in html page
     * @var array 
     */
    private $scripts = array();
    
    /**
     * Stylsheets to be included in html page
     * @var array
     */
    private $css = array();
    
    /**
     * Meta data included in the <head> marker
     * @var array
     */
    private $metas = array();
    
    /**
     * Rendered view
     * @var View
     */
    public $view = null;

	/**
	 * Determine if layout is enabled or not
	 * @var boolean
	 */
	public $enabled = true;
    
    public function __construct ()
    {
        $this->name = self::DEFAULT_LAYOUT_NAME;
        $this->setLayout($this->name);
    }
    
    public function setLayout ($name)
    {
        $path = LAYOUT_PATH.'/'.strtolower($name);
        if (is_dir($path)) {
            $this->name = strtolower($name);
            $this->path = $path;
        } else {
            throw new Mac_Layout_Exception_DirectoryNotFound('Unable to find layout directory for '.$name.' following path '.$path);
        }
    }
    
    public function setHeadTitle ($title)
    {
        $this->headTitle = $title;
    }
    
    public function renderHeadTitle ()
    {
        return '<title>'.$this->headTitle.'</title>'."\r\n";
    }
    
    public function addScript ($path)
    {
        $this->scripts[] = FRONT_JAVASCRIPT_PATH . (substr($path, 0, 1) == '/') ? $path : '/'.$path;
    }
    
    public function renderScript ()
    {
        $scripts = '';
        foreach($this->scripts as $script_path) {
            $scripts .= '<script type="text/javascript" src="'.$this->cleanTagAttribute($script_path).'"></script>'."\r\n";
        }
        return $scripts;
    }
    
    public function addStylesheet ($path, $media=null)
    {
        $this->css[FRONT_CSS_PATH . ((substr($path, 0, 1) == '/') ? $path : '/'.$path)] = $media;
    }
    
    public function renderHeadStylesheet ()
    {
        $stylesheets = '';
        foreach($this->css as $css_path => $media) {
            $stylesheets .= '<link href="'.$this->cleanTagAttribute($css_path).'" rel="stylesheet" type="text/css"'.(($media!==null) ? ' media="'.$this->cleanTagAttribute($media).'"' : '').' />'."\r\n";
        }
        return $stylesheets;
    }
    
    public function setMeta ($name, $value)
    {
        $this->metas[$name] = $value;
    }
    
    public function renderHeadMeta ()
    {
        $meta = '';
        foreach($this->metas as $name => $content) {
            $meta .= '<meta name="'.$this->cleanTagAttribute($name).'" content="'.$this->cleanTagAttribute($content).'" />'."\r\n";
        }
    }
    
    public function initView ($module, $controller, $action)
    {
        $view = new Mac_View(MODULES_PATH.'/'.$module.'/view/'.$controller.'/'.$action.'.phtml');
        $this->setView($view);
    }
    
    public function setView (Mac_View $view)
    {
        $this->view = $view;
    }
    
    public function getView ()
    {
        return $this->view;
    }
    
    public function renderView ()
    {
    	return $this->view->content();
    }
    
    /**
     * 
     * Render a phtml template
     * @param string $filename File name
     * @throws Layout_Exception_ScriptNotFound
     */
    public function render ($filename)
    {
        $render_path = $this->path.'/'.$filename;
        if (is_file($render_path)) {
            ob_start();
            include $render_path;
            $contents = ob_get_contents();
            ob_end_clean();
            return $contents;
        } else {
            throw new Mac_Layout_Exception_ScriptNotFound('Unable to render script '.$filename.'. File not found at '.$render_path);
        }
    }
    
    /**
     * Return layout html content
     * @throws Layout_Exception_ScriptNotFound
     * @return string
     */
    public function content ()
    {
		if ($this->enabled === true) {
			$render_path = $this->path.'/layout.phtml';
			
			if (is_file($render_path)) {
			    ob_start();
				include $render_path;
				$contents = ob_get_contents();
				ob_end_clean();
			} else {
				throw new Mac_Layout_Exception_ScriptNotFound('Unable to render Layout script layout.phtml. File not found at '.$render_path);
			}
		} else {
			$contents = $this->getView()->content();
		}
		return $contents;
    }

    /**
     * Disable layout output
     */
	public function disable ()
	{
		$this->enabled = false;
	}

	/**
	 * Enable layout output
	 */
	public function enable ()
	{
		$this->enabled = true;
	}
    
    private function cleanTagAttribute ($value)
    {
        return str_replace('"', '', $value);
    }
}
